package com.glsc.ngateway.platform.controller.it.networkassets;

import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.api.platform.dto.it.networkassets.DashboardDto;
import com.glsc.ngateway.common.api.common.dto.PageQueryParamDto;
import com.glsc.ngateway.common.api.platform.dto.it.networkassets.NetworkAssetMaintenanceDto;
import com.glsc.ngateway.platform.service.it.networkAssets.NetworkAssetMaintenanceService;
import com.glsc.ngateway.platform.utils.report.ReportFileConfigEnum;
import com.glsc.ngateway.platform.utils.report.ReportUploadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author xuchang
 */
@RestController
@RequestMapping("/it/networkAssets/networkAssetMaintenance")
@Api(value = "网络资产-网络资产维护", tags = {"网络资产"})
public class NetworkAssetMaintenanceController {
    private static final Logger logger = LoggerFactory.getLogger(NetworkAssetMaintenanceController.class);

    @Autowired
    private NetworkAssetMaintenanceService networkAssetMtService;

    /**
     * 查询
     */
    @PostMapping("/search")
    @ApiOperation(value = "查询网络资产维护", notes = "查询网络资产维护")
    public PlatformResponse<Page<NetworkAssetMaintenanceDto>> query(@RequestBody PageQueryParamDto<NetworkAssetMaintenanceDto> queryParam) throws Exception {
        return PlatformResponse.successData(networkAssetMtService.query(queryParam));
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除网络资产维护", notes = "删除网络资产维护")
    public PlatformResponse<Void> delete(@RequestBody NetworkAssetMaintenanceDto param) {
        networkAssetMtService.delete(param.getId());
        return PlatformResponse.successMsg("删除成功");
    }

    /**
     * 添加或修改
     */
    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "添加或修改网络资产维护", notes = "添加或修改网络资产维护")
    public PlatformResponse<Void> addOrUpdate(@RequestBody NetworkAssetMaintenanceDto param) {
        networkAssetMtService.addOrUpdate(param);
        return PlatformResponse.successMsg("添加成功");
    }

    /**
     * 导出数据
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出报表", notes = "导出报表")
    public byte[] export(@RequestBody NetworkAssetMaintenanceDto param, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;" + "filename=" + URLEncoder.encode(ReportFileConfigEnum.NETWORK_ASSET_MAINTENANCE.getFileName(), "UTF-8"));
        return networkAssetMtService.export(param);
    }

    /**
     * 导入编辑
     */
    @PostMapping("/upload")
    @ApiOperation(value = "导入编辑", notes = "导入编辑")
    public PlatformResponse<?> upload(@RequestPart("file") MultipartFile file) throws IOException {
        ReportUploadContext<NetworkAssetMaintenanceDto, String> context = networkAssetMtService.upload(file);
        return context.isCheckPass() ? PlatformResponse.success() : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    /**
     * dashboard展示
     * 第一张柱状图以【网络资产维护】表中vendor字段进行count数量统计，
     * 第二张柱状图，以【网络资产维护】表中status字段进行count数量统计，
     * 第三张柱状图以【网络资产维护】表中manufactureDate字段进行计算，与当前日期相差的年数<=3、>3且<=5、>5且<=6、>6四个维度来进行count数量统计
     */
    @GetMapping("/dashboard")
    @ApiOperation(value = "dashboard展示", notes = "dashboard展示")
    public PlatformResponse<DashboardDto> getDashBoard(){
        return PlatformResponse.successData(networkAssetMtService.getDashboard());
    }
}
